package com.blb.java08springboot.util;

import com.blb.java08springboot.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器，没有登录强制跳转登录页面
 */
public class LoginInterceptor implements HandlerInterceptor {

    //前置处理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session有没有user对象
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            response.sendRedirect("/toLogin");
            return false;
        }
        //false代表拦截，true代表放行
        return true;
    }
}
